﻿using aier.business.logic;
using aier.dataaccess.model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http;

namespace aier.wx.api.selfhost.Controllers
{
    //[Authorize]
    [RoutePrefix("api/Recommend")]
    public class RecommendController : ApiController
    {
        public RecommendBll recommendBll { get; set; }

        public CommonBll commonBll { get; set; }

        public ApiTool apiTool { get; set; }
        /// <summary>
        /// 查询推荐记录
        /// 查找条件：推荐记录ID
        /// 原程序 RecommendServiceImpl/qryRecommend
        /// </summary>
        /// <param name="recommendid"></param>
        /// <returns></returns>
        [HttpGet]
        [ActionName("QueryRecommendById")]
        public IHttpActionResult QueryRecommendById(Guid recommendid)
        {
            RecommendView recommendView = null;
            recommendView = recommendBll.QueryRecommendById(recommendid);
            return Ok<RecommendView>(recommendView);
        }
        /// <summary>
        /// 查询推荐记录
        /// 查找条件：contactid,ownerid
        /// 原程序 RecommendServiceImpl/qryRecommendList
        /// </summary>
        /// <param name="contactid"></param>
        /// <param name="ownerid"></param>
        /// <returns></returns>
        [HttpGet]
        [ActionName("QueryRecommendList")]
        public IHttpActionResult QueryRecommendList(Guid contactid, Guid ownerid)
        {
            List<RecommendView> recommendViews = null;
            recommendViews = recommendBll.QueryRecommendList(contactid, ownerid);
            return Ok<List<RecommendView>>(recommendViews);
        }

        /// <summary>
        /// 查询推荐记录
        /// 查找条件：startDateStr,endDateStr,istohospital,ownerid
        /// 原程序 RecommendServiceImpl/qryRecommendList
        /// </summary>
        /// <param name="startDateStr"></param>
        /// <param name="endDateStr"></param>
        /// <param name="istohospital"></param>
        /// <param name="state">0全部1已过期2三天内3已完成</param>
        /// <param name="ownerid"></param>
        /// <returns></returns>
        [HttpGet]
        [ActionName("QueryRecommendListByConditions")]
        public IHttpActionResult QueryRecommendListByConditions(string startDateStr, string endDateStr,string contactid,int state = 0, bool?istohospital=null)
        {
            UserInfo userInfo = apiTool.GetLoginUser(RequestContext);
            Guid cguid = Guid.Empty;
            if (!string.IsNullOrWhiteSpace(contactid)) {
                cguid = new Guid(contactid);
            }
            var recommendViews =  recommendBll.QueryRecommendListByConditions(startDateStr, endDateStr
                , userInfo.siteid.ToString(), userInfo.userid, cguid, state, istohospital);
            return Ok(recommendViews);
        }

        
        /// <summary>
        /// 增加推荐记录
        /// 原程序 RecommendServiceImpl/addRecommend
        /// </summary>
        /// <param name="recommendModel"></param>
        /// <returns></returns>
        [HttpPost]
        [ActionName("AddRecommend")]
        public IHttpActionResult AddRecommend(RecommendModel recommendModel)
        {
            ResResult resResult = recommendBll.AddRecommend(recommendModel);
            return Ok(resResult);
        }
        /// <summary>
        ///  删除推荐记录
        ///  RecommendServiceImpl/delRecommend
        /// </summary>
        /// <param name="recommendid"></param>
        /// <returns></returns>
        [HttpPost]
        [ActionName("DelRecommend")]
        public IHttpActionResult DelRecommend(Guid recommendid)
        {
            ResResult resResult = recommendBll.DelRecommend(recommendid);
            return Ok(resResult);
        }
        /// <summary>
        /// 修改推荐记录
        /// 原程序 RecommendServiceImpl/modRecommend
        /// </summary>
        /// <param name="recommendModel"></param>
        /// <returns></returns>
        [HttpPost]
        [ActionName("UpdateRecommend")]
        public IHttpActionResult UpdateRecommend(RecommendModel recommendModel)
        {
            ResResult resResult = recommendBll.UpdateRecommend(recommendModel);
            return Ok(resResult);
        }
        /// <summary>
        /// 创建 推荐记录、潜在客户、预约记录
        /// recommendInfo不用传leadid，leadInfo传入姓名和手机号码就可以
        /// 原程序 RecommendServiceImpl/addRecommend
        /// </summary>
        /// <param name="recommend">推荐记录</param>
        /// <returns>返回推荐记录ID</returns>
        [HttpPost]
        [ActionName("AddRecommendAndLead")]
        public IHttpActionResult AddRecommendAndLead(RecommendView recommend)
        {
            UserInfo userInfo = apiTool.GetLoginUser(RequestContext);
            ResResult resResult = commonBll.AddRecommendAndLead(recommend, userInfo);
            return Ok(resResult);
        }
        /// <summary>
        /// 修改 推荐记录、潜在客户、预约记录
        /// recommendInfo不用传leadid，leadInfo传入姓名和手机号码就可以
        /// 原程序 RecommendServiceImpl/addRecommend
        /// </summary>
        /// <param name="recommend">推荐记录</param>
        /// <param name="isModReservation">isModReservation=1则修改最后一次预约对象</param>
        /// <returns>返回推荐记录ID</returns>
        [Authorize]
        [HttpPost]
        [ActionName("UpdateRecommendAndLead")]
        public IHttpActionResult UpdateRecommendAndLead(RecommendView recommend,string isModReservation="1")
        {
            UserInfo userInfo = apiTool.GetLoginUser(RequestContext);
            ResResult resResult = commonBll.UpdateRecommendAndLead(recommend, userInfo, isModReservation);
            return Ok(resResult);
        }

    }
}
